package com.scqzy.shengchunqiang.sword2offer.day12;

import com.scqzy.shengchunqiang.structure.ListNode;

import java.util.Objects;

/**
 * @Description: 剑指 Offer 25. 合并两个排序的链表
 * @Author 盛春强
 * @Date 2021/8/19 17:28
 */
public class MergeTwoLists {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode node = new ListNode(Integer.MIN_VALUE);
        ListNode head = node;
        while (Objects.nonNull(l1)&&Objects.nonNull(l2)) {
            if (l1.val<l2.val){
                head.next = l1;
                l1 = l1.next;
            } else {
                head.next = l2;
                l2 = l2.next;
            }
            head = head.next;
        }
        if (Objects.nonNull(l1)) {
            head.next = l1;
        }
        if (Objects.nonNull(l2)) {
            head.next = l2;
        }
        return node.next;
    }
}
